home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / environ / getenv2.asm < prev    next >
Assembly Source File  |  1988-01-01  |  2KB  |  77 lines

  1.  
  2.  
  3. ;    getenv2.asm
  4. ;
  5. ;         This routine finds the system wide environment string
  6. ;    and returns its segment and size. Getenv2 was written to be
  7. ;    called from C, and requires the large memory model. When called
  8. ;    as getenv2(&envseg), envseg will contain the segment of the environment.
  9. ;    The value of getenv2 will be the environment size.
  10. ;    Compiled with Microsoft Macro Assembler ver 4.0 and written to be 
  11. ;    called from Microsoft C.
  12. ;
  13.  
  14. PSPSEG    segment    para at 0
  15.     org 014H        ;segment address of critical error vector
  16. ERRSEG    dw    ?
  17. PSPSEG    ends
  18.  
  19. INCLUDE    HEAD.H
  20.  
  21.     public    _getenv2
  22. _TEXT    segment
  23. _GETENV2    proc    far
  24.     push    bp
  25.     mov    bp,sp
  26.     push    ds
  27.     push    di
  28.     
  29.     mov    ax,__psp    ;get the psp segment saved by Microsoft and put
  30.     mov     es,ax        ;it in es for getting critical error segment.
  31.     assume    es:pspseg    ;let the assembler know this.
  32.     mov     dx,errseg    ;get the calling program's segment from the
  33.     mov    es,dx        ;error vector and put it in es.
  34.     mov     dx,es:[0014h]    ;get Command.com's critical error segment 
  35.     mov    es,dx        ;and put it in es.
  36.             
  37.     mov    bx,word ptr es:[002CH]    ;Command.com's environment segment
  38.     cmp    bx,0        ;vector should be zero.
  39.     jnz    failed        ;if not then we have failed.
  40.     
  41.     mov    bx,dx        ;if it is, then assume the environment string
  42.     dec    bx        ;follows this program.  Point es at
  43.     mov    es,bx        ;the segment allocation info, and
  44.     add    bx,word ptr es:[0003]    ;skip forward to next alloc segment.
  45.     add    bx,2        ;must add 2 paras to get over info.
  46.     
  47.     les    di,[bp+6]    ;get address of argument pointer-a far pointer
  48.     mov    es:[di],bx    ;return environment segment, after that
  49.     dec    bx        ;backup 16 bytes from environment to
  50.     mov    es,bx        ;get the segment allocation info.
  51.     mov    ax,word ptr es:[0003]    ;fetch size of environment segment,
  52.     shl    ax,1        ;multiply that by 16,
  53.     shl    ax,1        ;to get the environment string size.
  54.     shl    ax,1
  55.     shl    ax,1
  56.     pop    di
  57.     pop    ds
  58.     pop    bp
  59.     ret
  60.  
  61. failed:    ;Could not locate the environment
  62.     mov    dx,offset DGROUP:message
  63.     mov    ah,9
  64.     int    21h        ;Use Dos to print failure message.
  65.     pop    di
  66.     pop    ds
  67.     pop    bp
  68.     ret
  69. _GETENV2    endp
  70. _TEXT    ends
  71.  
  72. _DATA    segment
  73.     extrn    __psp:word    ;Microsoft stores the psp segment here.
  74. message    db    'Getenv2 could not locate environment',0dh,0ah,'$'
  75. _DATA    ends
  76.     end
  77.